#include <vector>

using namespace std;

class Solution {
public:
    int subarraySum(vector<int>& nums, int k) {
        int sum = 0;
        int ans = 0;
        unordered_map<int, int> map;
        map[0] = 1;
        for (int& num : nums) {
            sum += num;
            int val = sum - k;
            if (map.find(val) != map.end()) {
                ans += map[val];
            }
            map[sum]++;
        }
        return ans;
    }
};
